<?php
$CI = &get_instance();
if($CI->session->userdata('dsdhngay'))
	$ngay = $CI->session->userdata('dsdhngay');
else
	$ngay = time();
if($CI->session->userdata('dsdhtrangthai'))
	$trangthai = $CI->session->userdata('dsdhtrangthai');
else
	$trangthai = 0;
?>
<div class="folder-content02 Folderbg">
	<form method="post">
    	Mã đơn hàng: <input type="text" name="id" value="<?=$CI->session->userdata('dsdhid')?>" />
    	Ngày gửi: <?php echo form_date(array('name'=>'ngay', 'value'=>$ngay)); ?>
        Trạng thái:
        	<select name="trangthai">
            	<option value="-1" <?php echo ($trangthai==-1)?'selected':''?>>Tất cả</option>
            	<option value="0" <?php echo ($trangthai==0)?'selected':''?>>Chưa duyệt</option>
                <option value="1" <?php echo ($trangthai==1)?'selected':''?>>Đã duyệt</option>
            </select>
            <input type="submit" name="filter" value="Lọc"/>
    <table border="1" cellpadding="0" cellspacing="0" width="100%">
    <!--		bat dau tieu de bang  --->
        <tr>
            <th class="tieudebangsim">
                ID
            </th>
            <th class="tieudebangsim">
                Tài khoản
            </th>
            <th class="tieudebangsim">
                Họ tên
            </th>
            <th>
                Điện thoại
            </th>
            <th>
                Ngày gửi
            </th>
            <th>
            	Trạng thái
            </th>
            <th>
            	Xóa
            </th>
        </tr>
    <!--        ket thuc tieu de bang  -->
<?php
		
        $CI->load->database();
		$CI->db->select('tbl_donhang.id, taikhoan,hoten,sodienthoai,ngaygui,trangthai');
		$CI->db->from('tbl_donhang');
		$CI->db->join('tbl_thanhvien', 'tbl_donhang.id_thanhvien=tbl_thanhvien.id');
		$strquery = 'select count(id) soluong from tbl_donhang where true';
		
		if($CI->session->userdata('dsdhid'))
		{
			$CI->db->where('tbl_donhang.id', $CI->session->userdata('dsdhid'));
			$strquery .= ' and tbl_donhang.id=' . $CI->session->userdata('dsdhid');
		}
		else
		{
			if(isset($per_page))
				$CI->db->limit($per_page, $CI->uri->segment(4));
			if($trangthai >= 0)
			{
				$CI->db->where('trangthai', $trangthai);
				$strquery .= ' and trangthai=' . $trangthai;
			}
			$CI->db->where('day(ngaygui)=' . date('d', $ngay) . ' and month(ngaygui)=' . date('m', $ngay) . ' and year(ngaygui)=' . date('Y', $ngay));
			$strquery .= ' and day(ngaygui)=' . date('d', $ngay) . ' and month(ngaygui)=' . date('m', $ngay) . ' and year(ngaygui)=' . date('Y', $ngay);
		}
        $query = $CI->db->get();
		$danhsachkhachhang = $query->result();
		
		$query = $CI->db->query($strquery);
		$soluong = $query->result();
		$soluong = $soluong[0]->soluong;
		
        foreach($danhsachkhachhang as $r)
        {
			$temp = split(' ', $r->ngaygui);
			$temp1 = split('-', $temp[0]);
			$year = $temp1[0];
			$month = $temp1[1];
			$day = $temp1[2];
?>
            <tr>
            	<td><?=$r->id?></td>
                <td><a href="<?=site_url('admin/quanly/chitietdonhang/'.$r->id)?>" style="width:100%"><?=$r->taikhoan?></a></td>
                <td><?=$r->hoten?></td>
                <td><?=$r->sodienthoai?></td>
                <td><?= $day . '/' . $month . '/' . $year ?></td>
                <td><?=$r->trangthai?'Đã duyệt':'Chưa duyệt'?></td>
                <td><input type="submit" name="xoa/<?=$r->id?> " value="Xóa"/></td>
            </tr>
<?php
        }
?>
    </table>
    </form>
    <div align="center">
<?php
        if(isset($per_page))
        {
            $CI->load->library('pagination');
            $config = array();
            $config['base_url'] = site_url('admin/quanly/danhsachdonhang');
            $config['total_rows'] = $soluong;
            $config['per_page'] = $per_page;
            $config['first_link'] = 'Đầu';
            $config['prev_link'] = 'Trước';
            $config['next_link'] = 'Sau';
            $config['last_link'] = 'Cuối';
            $config['uri_segment'] = 4;
            $CI->pagination->initialize($config);
            echo $CI->pagination->create_links();
        }
?>
    </div>
</div>